<template>
  <div class="settings-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h1 class="page-title">系统设置</h1>
        <p class="page-description">配置博客系统的基本设置</p>
      </div>
      <div class="header-actions">
        <el-button
          type="primary"
          @click="saveSettings"
          :loading="saving"
          class="save-btn"
        >
          <el-icon><Check /></el-icon>
          保存设置
        </el-button>
        <el-button @click="resetSettings" class="reset-btn">
          <el-icon><Refresh /></el-icon>
          重置
        </el-button>
      </div>
    </div>

    <!-- 设置导航 -->
    <div class="settings-nav">
      <el-card class="nav-card" shadow="hover">
        <div class="nav-items">
          <div
            v-for="tab in tabs"
            :key="tab.key"
            :class="['nav-item', { active: activeTab === tab.key }]"
            @click="activeTab = tab.key"
          >
            <div class="nav-icon">
              <el-icon>
                <component :is="tab.icon" />
              </el-icon>
            </div>
            <div class="nav-content">
              <div class="nav-title">{{ tab.title }}</div>
              <div class="nav-description">{{ tab.description }}</div>
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 设置内容区域 -->
    <div class="settings-container">
      <!-- 设置内容 -->
      <div class="settings-content">
        <el-card class="content-card" shadow="never">
          <!-- 基本设置 -->
          <div v-if="activeTab === 'general'" class="settings-section">
            <div class="section-header">
              <h3 class="section-title">基本设置</h3>
              <p class="section-description">配置网站的基本信息和全局设置</p>
            </div>

            <el-form
              :model="generalSettings"
              label-position="top"
              class="settings-form"
            >
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="网站名称">
                    <el-input
                      v-model="generalSettings.siteName"
                      placeholder="请输入网站名称"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="管理员邮箱">
                    <el-input
                      v-model="generalSettings.adminEmail"
                      placeholder="请输入管理员邮箱"
                    />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-form-item label="网站描述">
                <el-input
                  v-model="generalSettings.siteDescription"
                  type="textarea"
                  :rows="3"
                  placeholder="请输入网站描述"
                />
              </el-form-item>

              <el-form-item label="网站关键词">
                <el-input
                  v-model="generalSettings.siteKeywords"
                  placeholder="请输入网站关键词，用逗号分隔"
                />
              </el-form-item>

              <el-form-item label="时区">
                <el-select
                  v-model="generalSettings.timezone"
                  placeholder="选择时区"
                  style="width: 100%"
                >
                  <el-option value="Asia/Shanghai" label="北京时间 (UTC+8)" />
                  <el-option value="UTC" label="世界标准时间 (UTC)" />
                  <el-option
                    value="America/New_York"
                    label="纽约时间 (UTC-5)"
                  />
                  <el-option value="Europe/London" label="伦敦时间 (UTC+0)" />
                </el-select>
              </el-form-item>
            </el-form>
          </div>

          <!-- 外观设置 -->
          <div v-if="activeTab === 'appearance'" class="settings-section">
            <div class="section-header">
              <h3 class="section-title">外观设置</h3>
              <p class="section-description">配置网站的外观和显示设置</p>
            </div>

            <el-form
              :model="appearanceSettings"
              label-position="top"
              class="settings-form"
            >
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="主题色">
                    <el-color-picker
                      v-model="appearanceSettings.primaryColor"
                    />
                    <span class="color-preview">{{
                      appearanceSettings.primaryColor
                    }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="每页文章数">
                    <el-input-number
                      v-model="appearanceSettings.postsPerPage"
                      :min="1"
                      :max="50"
                    />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="显示摘要">
                    <el-switch v-model="appearanceSettings.showExcerpt" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="显示阅读时间">
                    <el-switch v-model="appearanceSettings.showReadTime" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>

          <!-- SEO设置 -->
          <div v-if="activeTab === 'seo'" class="settings-section">
            <div class="section-header">
              <h3 class="section-title">SEO设置</h3>
              <p class="section-description">配置搜索引擎优化相关设置</p>
            </div>

            <el-form
              :model="seoSettings"
              label-position="top"
              class="settings-form"
            >
              <el-form-item label="默认Meta描述">
                <el-input
                  v-model="seoSettings.defaultMetaDescription"
                  type="textarea"
                  :rows="3"
                  placeholder="请输入默认的Meta描述"
                />
              </el-form-item>

              <el-form-item label="Google Analytics ID">
                <el-input
                  v-model="seoSettings.googleAnalyticsId"
                  placeholder="请输入Google Analytics跟踪ID"
                />
              </el-form-item>

              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="启用Sitemap">
                    <el-switch v-model="seoSettings.enableSitemap" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="启用RSS">
                    <el-switch v-model="seoSettings.enableRss" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>

          <!-- 统计代码设置 -->
          <div v-if="activeTab === 'analytics'" class="settings-section">
            <div class="section-header">
              <h3 class="section-title">统计代码设置</h3>
              <p class="section-description">配置网站统计和分析代码</p>
            </div>

            <el-form
              :model="analyticsSettings"
              label-position="top"
              class="settings-form"
            >
              <el-form-item label="启用统计代码">
                <el-switch v-model="analyticsSettings.enabled" />
              </el-form-item>

              <el-form-item
                label="Head 部分代码"
                v-if="analyticsSettings.enabled"
              >
                <el-input
                  v-model="analyticsSettings.headCode"
                  type="textarea"
                  :rows="6"
                  placeholder="请输入要插入到 HTML &lt;head&gt; 部分的代码，例如 Google Analytics 代码"
                />
                <div class="form-help">
                  这些代码将插入到每个页面的 &lt;head&gt; 标签内，通常用于放置
                  Google Analytics、百度统计等跟踪代码。
                </div>
              </el-form-item>

              <el-form-item
                label="Body 结尾部分代码"
                v-if="analyticsSettings.enabled"
              >
                <el-input
                  v-model="analyticsSettings.bodyCode"
                  type="textarea"
                  :rows="6"
                  placeholder="请输入要插入到 HTML &lt;/body&gt; 之前的代码"
                />
                <div class="form-help">
                  这些代码将插入到每个页面 body
                  标签的末尾，通常用于放置性能监控或第三方脚本。
                </div>
              </el-form-item>
            </el-form>
          </div>

          <!-- 邮件设置 -->
          <div v-if="activeTab === 'email'" class="settings-section">
            <div class="section-header">
              <h3 class="section-title">邮件设置</h3>
              <p class="section-description">配置邮件服务器和通知设置</p>
            </div>

            <el-form
              :model="emailSettings"
              label-position="top"
              class="settings-form"
            >
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="SMTP服务器">
                    <el-input
                      v-model="emailSettings.smtpHost"
                      placeholder="例如：smtp.gmail.com"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="SMTP端口">
                    <el-input-number
                      v-model="emailSettings.smtpPort"
                      :min="1"
                      :max="65535"
                    />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="用户名">
                    <el-input
                      v-model="emailSettings.smtpUser"
                      placeholder="请输入SMTP用户名"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="密码">
                    <el-input
                      v-model="emailSettings.smtpPassword"
                      type="password"
                      show-password
                      placeholder="请输入SMTP密码"
                    />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-form-item label="启用SSL">
                <el-switch v-model="emailSettings.enableSsl" />
              </el-form-item>
            </el-form>
          </div>

          <!-- 安全设置 -->
          <div v-if="activeTab === 'security'" class="settings-section">
            <div class="section-header">
              <h3 class="section-title">安全设置</h3>
              <p class="section-description">配置系统安全相关设置</p>
            </div>

            <el-form
              :model="securitySettings"
              label-position="top"
              class="settings-form"
            >
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="启用评论审核">
                    <el-switch
                      v-model="securitySettings.enableCommentModeration"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="登录失败限制">
                    <el-input-number
                      v-model="securitySettings.maxLoginAttempts"
                      :min="1"
                      :max="10"
                    />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="会话超时时间（分钟）">
                    <el-input-number
                      v-model="securitySettings.sessionTimeout"
                      :min="30"
                      :max="1440"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="启用两步验证">
                    <el-switch v-model="securitySettings.enableTwoFactor" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 图标导入
const { Check, Refresh, Setting, Brush, Search, Message, Lock } = await import(
  "@element-plus/icons-vue"
);

// Vue 相关导入
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";

// 响应式数据
const activeTab = ref("general");
const saving = ref(false);

// 标签配置
const tabs = [
  {
    key: "general",
    title: "基本设置",
    description: "网站基本信息配置",
    icon: Setting,
  },
  {
    key: "appearance",
    title: "外观设置",
    description: "网站外观和显示设置",
    icon: Brush,
  },
  {
    key: "seo",
    title: "SEO设置",
    description: "搜索引擎优化设置",
    icon: Search,
  },
  {
    key: "analytics",
    title: "统计代码",
    description: "网站统计代码配置",
    icon: "document", // 简化，不引入新图标
  },
  {
    key: "email",
    title: "邮件设置",
    description: "邮件服务器配置",
    icon: Message,
  },
  {
    key: "security",
    title: "安全设置",
    description: "系统安全配置",
    icon: Lock,
  },
];

// 设置数据
const generalSettings = ref({
  siteName: "我的博客",
  siteDescription: "一个基于Nuxt.js的现代化博客系统",
  siteKeywords: "blog,nuxt,vue,javascript",
  adminEmail: "admin@example.com",
  timezone: "Asia/Shanghai",
});

const appearanceSettings = ref({
  primaryColor: "#1890ff",
  postsPerPage: 10,
  showExcerpt: true,
  showReadTime: true,
});

const seoSettings = ref({
  defaultMetaDescription: "欢迎访问我的博客，这里分享技术文章和生活感悟",
  googleAnalyticsId: "",
  enableSitemap: true,
  enableRss: true,
});

const analyticsSettings = ref({
  headCode: "",
  bodyCode: "",
  enabled: false,
});

const emailSettings = ref({
  smtpHost: "smtp.gmail.com",
  smtpPort: 587,
  smtpUser: "",
  smtpPassword: "",
  enableSsl: true,
});

const securitySettings = ref({
  enableCommentModeration: true,
  maxLoginAttempts: 5,
  sessionTimeout: 120,
  enableTwoFactor: false,
});

// 方法
const saveSettings = async () => {
  saving.value = true;

  try {
    // 保存统计代码设置
    try {
      const analyticsResponse = (await $fetch("/api/admin/home/analytics", {
        method: "POST",
        body: analyticsSettings.value,
      })) as any;
      if (analyticsResponse.success) {
        console.log("统计代码设置保存成功");
      }
    } catch (error) {
      console.error("保存统计代码设置失败:", error);
    }

    // 其他设置保存
    await new Promise((resolve) => setTimeout(resolve, 1000));
    console.log("保存设置:", {
      general: generalSettings.value,
      appearance: appearanceSettings.value,
      seo: seoSettings.value,
      email: emailSettings.value,
      security: securitySettings.value,
      analytics: analyticsSettings.value,
    });
    ElMessage.success("设置保存成功！");
  } catch (error: any) {
    console.error("保存设置失败:", error);
    ElMessage.error(`保存失败: ${error.message}`);
  } finally {
    saving.value = false;
  }
};

const resetSettings = () => {
  // 重置当前标签页的设置
  ElMessage.info("设置已重置");
};

// 加载设置数据
const loadSettings = async () => {
  // 加载统计代码设置
  try {
    const analyticsResponse = (await $fetch(
      "/api/admin/home/analytics"
    )) as any;
    if (analyticsResponse.success) {
      analyticsSettings.value = {
        headCode: analyticsResponse.data.headCode || "",
        bodyCode: analyticsResponse.data.bodyCode || "",
        enabled: analyticsResponse.data.enabled || false,
      };
    }
  } catch (error) {
    console.error("加载统计代码设置失败:", error);
  }
};

// 页面加载时获取数据
onMounted(() => {
  loadSettings();
});

// 页面元数据
useHead({
  title: "系统设置 - 博客管理系统",
});
</script>

<style scoped>
.settings-page {
  transition: background-color 0.3s ease;
}

/* 暗黑模式背景 */
.dark {
  background-color: #1e293b;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  animation: fadeInDown 0.6s ease-out;
}

.header-content h1 {
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 8px 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-content p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

/* 暗黑模式文字颜色 */
.dark .header-content p {
  color: #cbd5e1;
}

.save-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  padding: 12px 24px;
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.save-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.reset-btn {
  border-radius: 8px;
  padding: 12px 24px;
  font-weight: 600;
  transition: all 0.3s ease;
}

/* 暗黑模式重置按钮 */
.dark .reset-btn {
  background-color: #374151;
  border-color: #4b5563;
  color: #e5e7eb;
}

.dark .reset-btn:hover {
  background-color: #4b5563;
  border-color: #6b7280;
}

/* 设置导航 */
.settings-nav {
  margin-bottom: 24px;
  animation: fadeInUp 0.6s ease-out 0.2s both;
}

.nav-card {
  border-radius: 12px;
  border: none;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

/* 暗黑模式导航卡片 */
.dark .nav-card {
  background: rgba(30, 41, 59, 0.95);
  border: 1px solid #334155;
}

.nav-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.dark .nav-card:hover {
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4);
}

.nav-items {
  display: flex;
  flex-direction: row;
  gap: 8px;
  overflow-x: auto;
  padding: 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid transparent;
  white-space: nowrap;
  min-width: 160px;
}

.nav-item:hover {
  background: rgba(102, 126, 234, 0.05);
  border-color: rgba(102, 126, 234, 0.2);
}

/* 暗黑模式导航项悬停 */
.dark .nav-item:hover {
  background: rgba(96, 165, 250, 0.1);
  border-color: rgba(96, 165, 250, 0.2);
}

.nav-item.active {
  background: linear-gradient(
    135deg,
    rgba(102, 126, 234, 0.1) 0%,
    rgba(118, 75, 162, 0.1) 100%
  );
  border-color: rgba(102, 126, 234, 0.3);
}

/* 暗黑模式活动导航项 */
.dark .nav-item.active {
  background: linear-gradient(
    135deg,
    rgba(96, 165, 250, 0.2) 0%,
    rgba(139, 92, 246, 0.2) 100%
  );
  border-color: rgba(96, 165, 250, 0.3);
}

.nav-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 16px;
  flex-shrink: 0;
}

.nav-title {
  font-weight: 600;
  color: #333;
  margin-bottom: 2px;
  font-size: 14px;
}

/* 暗黑模式导航标题 */
.dark .nav-title {
  color: #f1f5f9;
}

.nav-description {
  font-size: 12px;
  color: #666;
  line-height: 1.2;
}

/* 暗黑模式导航描述 */
.dark .nav-description {
  color: #94a3b8;
}

/* 设置容器 */
.settings-container {
  animation: fadeInUp 0.6s ease-out 0.4s both;
}

/* 设置内容 */
.content-card {
  border-radius: 12px;
  border: none;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
}

/* 暗黑模式内容卡片 */
.dark .content-card {
  background: rgba(30, 41, 59, 0.95);
  border: 1px solid #334155;
}

.settings-section {
  padding: 24px;
}

.section-header {
  margin-bottom: 32px;
}

.section-title {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 8px 0;
  color: #333;
}

/* 暗黑模式章节标题 */
.dark .section-title {
  color: #f1f5f9;
}

.section-description {
  color: #666;
  margin: 0;
  font-size: 14px;
}

/* 暗黑模式章节描述 */
.dark .section-description {
  color: #94a3b8;
}

.settings-form {
  max-width: 600px;
}

.color-preview {
  margin-left: 12px;
  font-size: 14px;
  color: #666;
  font-family: monospace;
}

.form-help {
  margin-top: 8px;
  padding: 8px 12px;
  background-color: #f8f9fa;
  border-radius: 6px;
  font-size: 12px;
  color: #6c757d;
  border-left: 3px solid #17a2b8;
}

/* 暗黑模式颜色预览 */
.dark .color-preview {
  color: #cbd5e1;
}

/* 暗黑模式下Element Plus组件的样式覆盖 */
.dark :deep(.el-form-item__label) {
  color: #f1f5f9;
}

.dark :deep(.el-input__inner) {
  background-color: #1e293b;
  border-color: #4b5563;
  color: #f1f5f9;
}

.dark :deep(.el-input__inner:focus) {
  border-color: #60a5fa;
}

.dark :deep(.el-textarea__inner) {
  background-color: #1e293b;
  border-color: #4b5563;
  color: #f1f5f9;
}

.dark :deep(.el-textarea__inner:focus) {
  border-color: #60a5fa;
}

.dark :deep(.el-select-dropdown) {
  background-color: #1e293b;
  border-color: #4b5563;
}

.dark :deep(.el-select-dropdown__item) {
  color: #f1f5f9;
}

.dark :deep(.el-select-dropdown__item:hover) {
  background-color: #374151;
}

.dark :deep(.el-input-number) {
  background-color: #1e293b;
  border-color: #4b5563;
}

.dark :deep(.el-input-number__increase),
.dark :deep(.el-input-number__decrease) {
  background-color: #374151;
  border-color: #4b5563;
  color: #f1f5f9;
}

.dark :deep(.el-switch__core) {
  background-color: #4b5563;
  border-color: #4b5563;
}

.dark :deep(.el-switch.is-checked .el-switch__core) {
  background-color: #60a5fa;
}

/* 动画效果 */
@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .settings-page {
    padding: 16px;
  }

  .page-header {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }

  .header-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
  }

  .nav-items {
    gap: 6px;
    padding: 6px;
  }

  .nav-item {
    padding: 12px 16px;
    min-width: 140px;
  }

  .nav-icon {
    width: 32px;
    height: 32px;
    font-size: 14px;
    margin-right: 8px;
  }

  .nav-title {
    font-size: 13px;
  }

  .nav-description {
    font-size: 11px;
  }
}

@media (max-width: 480px) {
  .settings-section {
    padding: 16px;
  }

  .section-title {
    font-size: 20px;
  }

  .settings-form :deep(.el-col) {
    width: 100%;
  }

  .nav-item {
    flex-direction: column;
    text-align: center;
    min-width: 100px;
    padding: 12px 8px;
  }

  .nav-icon {
    margin-right: 0;
    margin-bottom: 6px;
  }

  .nav-content {
    text-align: center;
  }

  .nav-title {
    font-size: 12px;
  }

  .nav-description {
    font-size: 10px;
  }
}
</style>
